/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.sx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.hcht.xrlh.rw.entity.DrRw;
import com.hcht.xrlh.sx.entity.DrSx;
import com.hcht.xrlh.sx.service.DrSxService;
import com.hcht.xrlh.zfdxk.entity.DrZfdxk;
import com.hcht.xrlh.zfdxk.service.DrZfdxkService;
import com.hcht.xrlh.zfdxktitle.entity.DrZfdxkTitle;
import com.hcht.xrlh.zfdxktitle.service.DrZfdxkTitleService;
import com.hcht.xrlh.zfryk.entity.DrZfryk;
import com.hcht.xrlh.zfryk.service.DrZfrykService;

/**
 * 双随机事项管理Controller
 * @author lrz
 * @version 2016-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/sx/drSx")
public class DrSxController extends BaseController {

	@Autowired
	private DrSxService drSxService;
	@Autowired
	private DrZfrykService drZfrykService;

	@Autowired
	private DrZfdxkService drZfdxkService;
	
	@Autowired
	private DrZfdxkTitleService drZfdxkTitleService;

	@Autowired
	private OfficeService officeService;
	
	@ModelAttribute
	public DrSx get(@RequestParam(required=false) String id) {
		DrSx entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drSxService.get(id);
		}
		if (entity == null){
			entity = new DrSx();
		}
		return entity;
	}
	
	@RequiresPermissions("sx:drSx:view")
	@RequestMapping(value = {"list", ""})
	public String list(DrSx drSx, HttpServletRequest request, HttpServletResponse response, Model model) {
		drSx.setOfficeZd(UserUtils.getUser().getOffice());
		for(Role role : UserUtils.getUser().getRoleList()){
			if("2".equals(role.getDataScope())){
				drSx.setOfficeZd(officeService.get("1"));
			}
		}
		drSx.setpIds(UserUtils.getUser().getOffice().getParentIds());
		drSx.setuFlag("1");
		Page<DrSx> page = drSxService.findPage(new Page<DrSx>(request, response), drSx); 
		model.addAttribute("page", page);
		return "xrlh/sx/drSxList";
	}

	@RequiresPermissions("sx:drSx:view")
	@RequestMapping(value = "form")
	public String form(DrSx drSx, Model model) {
		model.addAttribute("drSx", drSx);
		return "xrlh/sx/drSxForm";
	}

	
	@RequiresPermissions("sx:drSx:view")
	@RequestMapping(value = "formview")
	public String formview(DrSx drSx, Model model) {
		model.addAttribute("drSx", drSx);
		return "xrlh/sx/drSxFormview";
	}
	
	@RequiresPermissions("sx:drSx:view")
	@RequestMapping(value = "zdyform")
	public String zdyform(DrSx drSx, Model model) {
		model.addAttribute("drSx", drSx);
		return "xrlh/sx/drSxForm_zdy";
	}

	@RequiresPermissions("sx:drSx:view")
	@RequestMapping(value = "zdyform_tjj")
	public String zdyform_tjj(DrSx drSx, Model model) {
		model.addAttribute("drSx", drSx);
		return "xrlh/sx/drSxForm_zdy_tjj";
	}
	
	@RequiresPermissions("sx:drSx:view")
	@RequestMapping(value = "zdyform_tjjsx")
	public String zdyform_tjjsx(DrSx drSx, Model model) {
		model.addAttribute("drSx", drSx);
		return "xrlh/sx/drSxForm_tjjsx";
	}
	
	@RequiresPermissions("sx:drSx:view")
	@RequestMapping(value = "zdyformview")
	public String zdyformview(DrSx drSx, Model model) {
		model.addAttribute("drSx", drSx);
		return "xrlh/sx/drSxForm_zdyview";
	}
	
	@RequiresPermissions("sx:drSx:edit")
	@RequestMapping(value = "save")
	public String save(DrSx drSx, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, drSx)){
			return form(drSx, model);
		}
		String sxId = drSxService.save_r(drSx);
		addMessage(redirectAttributes, "保存双随机事项成功");
		if(request.getParameter("methodflag").equals("0")){
			return "redirect:"+Global.getAdminPath()+"/sx/drSx/?repage";
		}else{
			return "redirect:"+Global.getAdminPath()+"/sys/user/list_zj?sxId="+sxId;
			//return "modules/sys/userList_zj";
		}
	}
	
	@RequiresPermissions("sx:drSx:edit")
	@RequestMapping(value = "delete")
	public String delete(DrSx drSx, RedirectAttributes redirectAttributes) {
		drSxService.delete(drSx);
		addMessage(redirectAttributes, "删除双随机事项管理成功");
		return "redirect:"+Global.getAdminPath()+"/sx/drSx/?repage";
	}

	@RequestMapping(value = "fdry")
	public String fdry(DrSx drSx, Model model,HttpServletRequest request, HttpServletResponse response) {
		String sxId = request.getParameter("id"); 
		drSx.setId(sxId);
		
		DrSx sx = drSxService.get(sxId);
		
		DrZfryk ry = new DrZfryk();
		ry.setSxId(sxId);
		//Page<DrZfryk> page_ryk = drZfrykService.findPage(new Page<DrZfryk>(request, response), ry); 
		List<DrZfryk> list_ryk = drZfrykService.findList(ry);
	
		model.addAttribute("page_ryk", list_ryk);
		model.addAttribute("drSx", sx);
		return "xrlh/sx/fd_ry_view";
	}
	
	@RequestMapping(value = "fdsx")
	public String fdsx(DrSx drSx, Model model,HttpServletRequest request, HttpServletResponse response) {
		String sxId = request.getParameter("id"); 
		drSx.setId(sxId);
		
		DrSx sx = drSxService.get(sxId);
		
		
		DrZfdxk dx = new DrZfdxk();
		dx.setDrSx(sx);
		List<DrZfdxk> list_dxk = drZfdxkService.findList(dx);
		//Page<DrZfdxk> page_dxk = drZfdxkService.findPage(new Page<DrZfdxk>(request, response), dx); 
		List<DrZfdxkTitle> list_title = Lists.newArrayList();
		DrZfdxkTitle title = new DrZfdxkTitle();
		title.setSxId(sxId);
		list_title = drZfdxkTitleService.findList(title);
		model.addAttribute("list_title", list_title);
		model.addAttribute("page_dxk", list_dxk);
		return "xrlh/sx/fd_dx_view";
	}	
	
//-------------------
	@RequestMapping(value = "zdyry")
	public String zdyry(DrSx drSx, Model model,HttpServletRequest request, HttpServletResponse response) {
		String sxId = request.getParameter("id"); 
		drSx.setId(sxId);
		
		DrSx sx = drSxService.get(sxId);
		
		DrZfryk ry = new DrZfryk();
		ry.setSxId(sxId);
		//Page<DrZfryk> page_ryk = drZfrykService.findPage(new Page<DrZfryk>(request, response), ry); 
		List<DrZfryk> list_ryk = drZfrykService.findList(ry);
	
		model.addAttribute("page_ryk", list_ryk);
		model.addAttribute("drSx", sx);
		return "xrlh/sx/zdy_ry_view";
	}
	
	@RequestMapping(value = "zdysx")
	public String zdysx(DrSx drSx, Model model,HttpServletRequest request, HttpServletResponse response) {
		String sxId = request.getParameter("id"); 
		drSx.setId(sxId);
		
		DrSx sx = drSxService.get(sxId);
		
		
		DrZfdxk dx = new DrZfdxk();
		dx.setDrSx(sx);
		List<DrZfdxk> list_dxk = drZfdxkService.findList(dx);
		//Page<DrZfdxk> page_dxk = drZfdxkService.findPage(new Page<DrZfdxk>(request, response), dx); 
		List<DrZfdxkTitle> list_title = Lists.newArrayList();
		DrZfdxkTitle title = new DrZfdxkTitle();
		title.setSxId(sxId);
		list_title = drZfdxkTitleService.findList(title);
		model.addAttribute("list_title", list_title);
		model.addAttribute("page_dxk", list_dxk);
		return "xrlh/sx/zdy_dx_view";
	}	
}